﻿using System;
using System.Linq;
using System.Web.Mvc;
using Super.Common.Security;
using Super.Common.Validate;
using Super.Infrastructure;
using Super.DomainService;
using Super.Entities;
using Super.Common;
using Super.Mvc;

namespace Super.Web.Areas.Admin.Controllers
{
    public class UserController : BaseController
    {
        //
        // GET: /Admin/User/

        public ActionResult Index()
        {

            var adp = new DbContextAdapter();
            var userService = new BaseService<User>(adp);
            var page = Query.Params("pageNum");
            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }
            var where = PredicateBuilder.True<User>();
            where = where.And(a => a.IsDel == false);

            #region 查询

            var keyWord = Query.Post("keyword");

            var startTime = Query.Post("startTime") == "" ? DateTime.MinValue : Convert.ToDateTime(Query.Post("startTime"));
            var endTime = Query.Post("endTime") == "" ? DateTime.MinValue : Convert.ToDateTime(Query.Post("endTime"));

            ViewBag.keyword = keyWord;

            ViewBag.starTime = startTime;
            ViewBag.endTime = endTime;

            if (!string.IsNullOrEmpty(keyWord))
            {
                where = where.And(
                        a =>
                        a.Name.Contains(keyWord) ||
                        a.UID.Contains(keyWord) ||
                        a.Telephone.Contains(keyWord) ||
                        a.JianMa.Contains(keyWord));

            }

            if (startTime != DateTime.MinValue)
            {
                where = where.And(a => a.CreateTime >= startTime);
            }
            if (endTime != DateTime.MinValue)
            {
                endTime = endTime.AddDays(1);
                where = where.And(a => a.CreateTime <= endTime);
            }
            #endregion

            var user = userService.Repository.Find(where).OrderByDescending(a => a.ID).Page(page.ToInt(), PageSize);
            ViewBag.Records = user.ItemCount;//记录数
            //ViewBag.User= user.ItemCollection.ToList();
            BindPage(user.ItemCount, page.ToInt(), PageSize);
            return View(user.ItemCollection.ToList());
        }


        public ActionResult Edit()
        {
            var id = Query.Get("id").ToInt(0);
            if (id > 0)
            {
                var adp = new DbContextAdapter();
                var userService = new UserService(adp);
                var userModel = userService.GetUserById(id);
                return View(userModel);
            }

            return View();
        }

        [HttpPost]
        public JsonResult Edit(User model)
        {
            var adp = new DbContextAdapter();
            var userService = new UserService(adp);
            var passWord = Query.Post("PassWord");
            if (!string.IsNullOrEmpty(passWord))
            {
                model.PassWord = MD5.GET(passWord);
            }

            if (model.ID <= 0)
            {
                model.UID = Number.GetRandomNumber(8).ToString();
                model.CreateTime = DateTime.Now;

            }
            if (string.IsNullOrEmpty(model.JianMa))
            {
                var dicService = new DicService(adp);
                model.JianMa = dicService.GetChineseFirstSpell(model.Name);
            }
            userService.Repository.InsertOrUpdate(model);
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "userIndex");
        }

        public JsonResult Delete()
        {
            var id = Query.Get("id").ToInt(0);
            var adp = new DbContextAdapter();
            var userService = new UserService(adp);
            var userModel = userService.Repository.Find(a => a.ID == id).FirstOrDefault();

            if (userModel != null)
            {
                userService.Repository.Delete(userModel);
            }
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "userIndex");
        }



        public ActionResult SubSearch()
        {
            var keyword = Query.Post("keyword");
            var where = PredicateBuilder.True<User>();
            where = where.And(a => a.IsDel == false);
            if (!string.IsNullOrEmpty(keyword))
            {
               where= where.And(a => a.Name.Contains(keyword) || a.Telephone.Contains(keyword));
            }
            var adp = new DbContextAdapter();
            var userService = new UserService(adp);
            var users = userService.Repository.Find(where).OrderByDescending(a=>a.ID).ToList();
            ViewBag.Records = users.Count;//记录数
         
            return View(users);
        }

        #region 单个用户站内信发送
        public ActionResult SendMessage(int id)
        {
            ViewBag.userId = id;
            return View();
        }


        #endregion
    }
}
